package com.weetop.cfw.base.presenter.imp

import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.RegexUtils
import com.weetop.cfw.base.presenter.ForgetPasswordPresenter
import com.weetop.cfw.base.view.BaseView
import com.weetop.cfw.base.view.ForgetPasswordView
import com.weetop.cfw.bean.ErrorBean
import com.weetop.cfw.bean.VerificationCodeBean
import com.weetop.cfw.callback.RxJavaCallBack
import com.weetop.cfw.register.ForgetPasswordActivity
import com.weetop.cfw.utils.RetrofitUtils
import com.weetop.cfw.utils.RxJavaUtils

class ForgetPasswordPresenterImp(var forgetPasswordView: ForgetPasswordView? = null): ForgetPasswordPresenter {
    /**
     * 重置密码
     * @param mobile 手机号
     * @param userpwd 新密码
     * @param smscode 验证码
     */
    override fun resetPassword(
        forgetPasswordActivity: ForgetPasswordActivity,
        mobile: String,
        userpwd: String,
        makeSureUserpwd:String,
        smscode: String
    ) {
      if(mobile.isEmpty()){
          forgetPasswordView?.showNativeShortToast("请输入手机号")
      }else if(!RegexUtils.isMobileExact(mobile)){
          forgetPasswordView?.showNativeShortToast("请输入合法手机号")
      }else if(smscode.isEmpty()){
          forgetPasswordView?.showNativeShortToast("请输入验证码")
      }else if(userpwd.isEmpty()){
          forgetPasswordView?.showNativeShortToast("请输入新密码")
      }else if(makeSureUserpwd.isEmpty()){
          forgetPasswordView?.showNativeShortToast("请确认密码")
      }else if(userpwd != makeSureUserpwd){
          forgetPasswordView?.showNativeShortToast("两次输入密码不一致")
      }else{
          forgetPasswordActivity.showLoadingDialog(forgetPasswordActivity,"正在找回密码...")
          RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().forgetPassword(mobile, userpwd, smscode),forgetPasswordActivity,
              object : RxJavaCallBack<ErrorBean>() {
                  override fun onSuccess(t: ErrorBean?) {
                      forgetPasswordView?.resetPasswordSuccess(t)
                      forgetPasswordActivity.dismissLoadingDialog()
                  }

                  override fun onRequestError(t: ErrorBean?) {
                      super.onRequestError(t)
                      forgetPasswordActivity.dismissLoadingDialog()
                  }

                  override fun onErrorResponse(throwable: Throwable?) {
                      super.onErrorResponse(throwable)
                      forgetPasswordActivity.dismissLoadingDialog()
                  }

              })
      }
    }

    /**
     * 获取验证码
     * @param mobile 手机号
     */
    override fun getVerificationCode(
        forgetPasswordActivity: ForgetPasswordActivity,
        mobile: String
    ) {
        when {
            mobile.isEmpty() -> {
                forgetPasswordView?.showNativeShortToast("请输入手机号")
            }
            !RegexUtils.isMobileExact(mobile) -> {
                LogUtils.d("mobile",mobile)
                forgetPasswordView?.showNativeShortToast("请输入合法手机号")
            }
            else -> {
                RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().getVerificationCode(mobile),forgetPasswordActivity,object:
                    RxJavaCallBack<VerificationCodeBean>(){
                    override fun onSuccess(t: VerificationCodeBean?) {
                        LogUtils.d(t?.toString())
                        forgetPasswordView?.getVerificationCodeSuccess(t)
                    }
                })
            }
        }
    }

    override fun attachView(baseView: BaseView) {
        forgetPasswordView = baseView as? ForgetPasswordView
    }

    override fun detachView() {
       forgetPasswordView = null
    }
}